package com.sos.widget.view.recyclerview;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class DividerMargin extends RecyclerView.ItemDecoration {
    public static final int DIVIDER_ONE_TOP = -1;//只有顶部
    public static final int DIVIDER_TOP = 1;
    public static final int DIVIDER_BOTTOM = 2;//只有底部
    public static final int DIVIDER_Left_RIGHT = 3;//左右
    public static final int DIVIDER_TOP_Left_RIGHT = 4;//顶部+左右
    public static final int DIVIDER_BOTTOM_LEFT_RIGHT = 5;//底部+左右
    public static final int DIVIDER_ONE_BOTTOM = 7;//只有底部
    public static final int DIVIDER_RIGHT = 6;//右边
    public static final int DIVIDER_SPACE = 8;
    public static final int DIVIDER_SPACE_BOTTOM = 9;//不包含底部
    public static final int DIVIDER_Left = 10;//左边
    public static final int DIVIDER_ONE_LEFT_RIGHT = 10;//只有左右
    private int margin_space = 0;
    private int margin_top = 0;
    private int margin_bottom = 0;
    private int margin_left = 0;
    private int margin_right = 0;
    private int margin_start_end=0;
    private int mode = 0;

    public DividerMargin(int mode, int margin,int margin_start_end) {
        this.mode = mode;
        if (mode == DIVIDER_TOP || mode == DIVIDER_ONE_TOP) {
            this.margin_top = margin;
        }
        if (mode == DIVIDER_BOTTOM || mode == DIVIDER_ONE_BOTTOM||mode==DIVIDER_SPACE_BOTTOM) {
            this.margin_bottom = margin;
        }
        if (mode == DIVIDER_Left_RIGHT) {
            this.margin_left = margin;
            this.margin_right = margin;
        }
        if (mode == DIVIDER_RIGHT) {
            this.margin_right = margin;
        }
        if (mode == DIVIDER_SPACE) {
            this.margin_start_end=margin_start_end;
            this.margin_space = margin;
        }
        if (mode == DIVIDER_Left) {
            this.margin_left = margin;
        }
        if(mode==DIVIDER_ONE_LEFT_RIGHT){
            this.margin_space=margin;
        }

    }

    public DividerMargin(int mode, int margin) {
        this.mode = mode;
        if (mode == DIVIDER_TOP || mode == DIVIDER_ONE_TOP) {
            this.margin_top = margin;
        }
        if (mode == DIVIDER_BOTTOM || mode == DIVIDER_ONE_BOTTOM||mode==DIVIDER_SPACE_BOTTOM) {
            this.margin_bottom = margin;
        }
        if (mode == DIVIDER_Left_RIGHT) {
            this.margin_left = margin;
            this.margin_right = margin;
        }
        if (mode == DIVIDER_RIGHT) {
            this.margin_right = margin;
        }
        if (mode == DIVIDER_SPACE) {
            this.margin_space = margin;
        }
        if (mode == DIVIDER_Left) {
            this.margin_left = margin;
        }
        if(mode==DIVIDER_ONE_LEFT_RIGHT){
            this.margin_space=margin;
        }

    }

    public DividerMargin(int mode, int marginTb, int marginL, int marginR) {
        if (mode == DIVIDER_TOP_Left_RIGHT) {
            this.margin_top = marginTb;
            this.margin_left = marginL;
            this.margin_right = marginR;
        }
        if (mode == DIVIDER_BOTTOM_LEFT_RIGHT) {
            this.margin_bottom = marginTb;
            this.margin_left = marginL;
            this.margin_right = marginR;
        }

    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (mode == DIVIDER_ONE_TOP || mode == DIVIDER_ONE_BOTTOM||mode== DIVIDER_SPACE||mode==DIVIDER_ONE_LEFT_RIGHT) {
            if (layoutManager instanceof LinearLayoutManager&&!layoutManager.getClass().getSimpleName().equals("WrapContentGridLayoutManager")) {
                int position = parent.getChildAdapterPosition(view);
                if (mode == DIVIDER_ONE_TOP) {
                    if (position == 0) {
                        outRect.top = margin_top;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = margin_bottom;
                    } else {
                        outRect.top = 0;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = margin_bottom;
                    }
                }else if(mode==DIVIDER_ONE_BOTTOM){
                    if (position == (parent.getChildCount()-1)) {
                        outRect.top = margin_top;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = margin_bottom;
                    } else {
                        outRect.top = margin_top;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = 0;
                    }
                } else if(mode==DIVIDER_ONE_LEFT_RIGHT){
                    if(position==0){
                        outRect.top = margin_top;
                        outRect.left = margin_space;
                        outRect.right = margin_space/2;
                        outRect.bottom = margin_bottom;
                    }else if(position==layoutManager.getItemCount() - 1){
                        outRect.top = margin_top;
                        outRect.left = margin_space/2;
                        outRect.right = margin_space;
                        outRect.bottom = margin_bottom;
                    }else{
                        outRect.top = margin_top;
                        outRect.left = margin_space/2;
                        outRect.right = margin_space/2;
                        outRect.bottom = margin_bottom;
                    }
                } else {
                    if (position == layoutManager.getItemCount() - 1) {
                        outRect.top = margin_top;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = margin_bottom;
                    } else {
                        outRect.top = margin_top;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = 0;
                    }
                }

            } else if (layoutManager instanceof StaggeredGridLayoutManager || layoutManager instanceof GridLayoutManager) {
                int spanCount = 0;
                if (layoutManager instanceof StaggeredGridLayoutManager) {
                    StaggeredGridLayoutManager layoutManager1 = (StaggeredGridLayoutManager) layoutManager;
                    spanCount = layoutManager1.getSpanCount();
                } else if (layoutManager instanceof GridLayoutManager) {
                    GridLayoutManager layoutManager1 = (GridLayoutManager) layoutManager;
                    spanCount = layoutManager1.getSpanCount();
                }
                int position = parent.getChildAdapterPosition(view);
                if (mode == DIVIDER_SPACE) {
                    int space = margin_space / 2;
                    position += 1;
                    if (layoutManager instanceof StaggeredGridLayoutManager) {
                        StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
                        int spanIndex = layoutParams.getSpanIndex();

                        if (spanIndex == 0) {
                            // left
                            outRect.left = 0;
                            outRect.right =spanCount==1?0: space;
                            outRect.top =0;
                            outRect.bottom = 0;
                            if (spanCount==1&&position!=parent.getChildCount()){
                                outRect.top=space;
                            }
                        } else{
                            //right
                            outRect.right = 0;
                            outRect.left =spanCount==1?0:space;
                            outRect.top = 0;
                            outRect.bottom = 0;
                            if (spanCount==1&&position!=parent.getChildCount()){
                                outRect.bottom=space;
                            }
                        }
                    }else{
                        if(spanCount==3){
                            if (position % spanCount == 0) {
                                //最右边
                                outRect.left =space;
                                outRect.right = 0;
                                outRect.top = position>spanCount?margin_space:0;
                                outRect.bottom = 0;
                            } else if (position % spanCount == 1) {
                                //最左边
                                outRect.right = space;
                                outRect.left = 0;
                                outRect.top = position>spanCount?margin_space:0;
                                outRect.bottom = 0;
                            } else {
                                //最中间
                                outRect.left = space/2;
                                outRect.right =space/2;
                                outRect.top = position>spanCount?margin_space:0;
                                outRect.bottom = 0;
                            }
                        }else{
                            if (position % spanCount == 0) {
                                //最右边
                                outRect.left =spanCount==1?0: space;
                                outRect.right = 0;
                                outRect.top = position>spanCount?margin_space:0;
                                outRect.bottom = 0;
                            } else if (position % spanCount == 1) {
                                //最左边
                                outRect.right = spanCount==1?0: space;
                                outRect.left = 0;
                                outRect.top = position>spanCount?margin_space:0;
                                outRect.bottom = 0;
                            } else {
                                //最中间
                                outRect.left = spanCount==1?0: space;
                                outRect.right = spanCount==1?0: space;
                                outRect.top = position>spanCount?margin_space:0;
                                outRect.bottom = 0;
                            }
                        }

                    }

                } else {

                    if (position < spanCount) {
                        outRect.top = margin_top;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = margin_bottom;
                    } else {
                        outRect.top = 0;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = margin_bottom;
                    }
                }

            }

        } else {
            outRect.top = margin_top;
            outRect.left = margin_left;
            outRect.right = margin_right;
            outRect.bottom = margin_bottom;
            if(mode==DIVIDER_SPACE_BOTTOM&&layoutManager instanceof GridLayoutManager){
                GridLayoutManager layoutManager1 = (GridLayoutManager) layoutManager;
                int itemCount = layoutManager1.getItemCount();
                int spanCount = layoutManager1.getSpanCount();
                //最后一行的第一个
                int finalPosition=((itemCount/spanCount)-1)*spanCount;
                //判断是否最后一行
                if(parent.getChildAdapterPosition(view)>=finalPosition){
                    outRect.bottom=0;
                }
            }

        }

    }
}
